<html>

<head></head>
<script src="http://guangzhou.qilong.com/Public/common/js/web/lib/jquery-1.11.1.min.js"></script>
<script src="http://adminl.elite.com/static/admin/lib/jquery.md5.js?v=1"></script>
<body>

    <div style="width: 400px; height: 400px; float:right; background-color: #80FFFF; overflow: scroll;" id="ok">
        <ul>
            <li>成功11</li>
        </ul>
    
    </div>
    <div style="width: 400px; height: 400px; overflow: scroll;" id="error">
        <ul>
            <li>失败</li>
        </ul>
    </div>
    <div style="margin: 10px;">
        <select id="prince" name="prince">
        {volist name="arealist" id="vo"}
        <option value="{$vo['tid']}">{$vo['fullname']}</option>
        {/volist}
        </select>
        <a href="JavaScript:;" id="getSubStart">子区域开始{:count($arealist)}</a>
    </div>
    
    <div><a href="JavaScript:;" id="start">开始</a> </div>
<script>


    
    var list =  $('#prince option');
    var plist = Array();
    $.each(list,function(k,val){
        var pid = $(this).val();
        plist.push(pid);
    });
    
    var lockTime = 0;
    var num = 0;    
    var getSubByPid = function(pkey){
       // console.log('start pkey:'+pkey);
        var savepid = plist[pkey];
        if( savepid ){
            
            var myDate = new Date();
            var s = myDate.getSeconds();
            if( lockTime == 0 ){
                lockTime = s;
                console.log('开始');
               // getSubByPid(pkey+1);
                getSubLbs(savepid,pkey);
            }else{
                if( lockTime == s ){
                    if( num < 3 ){
                        num++;
                        console.log('还可以开始'+num);
                        //getSubByPid(pkey+1);                        
                        getSubLbs(savepid,pkey);
                    }else{
                        num = 0;
                        setTimeout(function(){
                            console.log('等待1秒');
                           // getSubByPid(pkey+1); 
                             getSubLbs(savepid,pkey);
                        },1000);
                    }
                }else{
                    lockTime = s;
                    console.log('执行');
                   // getSubByPid(pkey+1); 
                    getSubLbs(savepid,pkey);
                }
            }
           // getSubLbs(savepid,pkey);
        }else{
            console.log('结束');
        }
    }
    
    // 列表保存
    var thisSaveData;
    var pid;
    var subSaveLbs = function (subLists,pid,pkey){
        thisSaveData = subLists['result'][0];
        pid = pid;
        var eachSave = function (key){
            var tval = thisSaveData[key];
            //console.log(thisSaveData);
            //console.log(tval);
            if( tval ){
                tval.pid = pid;
               // console.log(tval)
               save( tval,key,eachSave );
            }else{
                getSubByPid(pkey+1);
            }
        }
        eachSave(0);
    }
    
    
    
    $('#getSubStart').on('click',function(){
        console.log(plist);
        getSubByPid(0);
    });
    
    
    
    // 获取子区域
    var errorList = Array();
    var nullList = Array();
    var tnum = 0;
    var getSubLbs = function (pid,pkey){
        var url = 'https://apis.map.qq.com/ws/district/v1/getchildren?id='+pid+'&key=GFDBZ-46CWX-UTT42-7QAXM-FHF55-Y7FDO';
        var data = {"output":"jsonp"};
        $.ajax({
            url:url,
            type:"get",
            dataType:"jsonp",
            data:data,
            jsonp:"callback",
            jsonpCallback:"QQmap",
            error:function(data,status){
                errorList.push(pid);
                Cache.set('errorList',JSON.stringify(errorList));
                console.log('网络异常');
                console.log(data);
                console.log(status);
            },
            success:function(data,status){
                //Cache.set('result_'+pid,JSON.stringify(data));
                
                
                if( data.status == 363 ){
                    console.log( 'log:腾讯返回错误 : ID 不存在'+pid);
                    console.log(data);
                    getSubByPid(pkey+1);
                                        
                }else if( data.status != 0   ){
                    console.log( 'log:腾讯返回错误');
                    console.log(data);
                    setTimeout(function(){
                        getSubLbs(pid,pkey);
                    },1000);                    
                }else{
                    tnum ++ ;
                    var ret = 'ture : ';                
                    if( ! data['result'][0] ){
                        nullList.push(pid);
                        Cache.set('nullList',JSON.stringify(nullList));
                        ret = 'false : ';
                    }
                    console.log('第'+tnum+'条请求数据 success : '+ret+pid);
                    
                    subSaveLbs(data,pid,pkey);
                }
            }
        }); 
    }
    
    






// ----------------------------
    var getLbs= function(){
        var url ='https://apis.map.qq.com/ws/district/v1/list?key=GFDBZ-46CWX-UTT42-7QAXM-FHF55-Y7FDO';
        //url = url+'&sig='+sig;
        var data = {"output":"jsonp"};
        $.ajax({
            url:url,
            type:"get",
            dataType:"jsonp",
            data:data,
            jsonp:"callback",
            jsonpCallback:"QQmap",
            error:function(data,status){
                console.log('网络异常');
                console.log(data);
                console.log(status);
            },
            success:function(data,status){
                Cache.set('result',JSON.stringify(data));
                console.log('success');
                console.log(data);
                var aa = Cache.get('result');
                console.log(aa);
            }
        });      
    }
    
    
    $('#start').on('click',function(){
        //getLbs();
        show();
    });
    
    var caseData ;
    var show = function (){
        var aa = Cache.get('result');
        var dd = JSON.parse(aa);
        caseData = dd.result[0];       
        
       // savePost(dd.result[1]);
        //console.log(dd.result);
       //  console.log(caseData);
       savePost(0);
    }
    
    
    var savePost = function (key){
        var saved = caseData[key];
        if( saved ){
            save(saved ,key);
        }
    }
    
    
    var save = function (data,key,bakFunc){
        var url ='http://adminl.elite.com/test/area/save';
        $.ajax({
            url:url,
           // async:false,
            data:data,
            dataType:'json',
            type:'post',
            error:function(){
                console.log('网络异常'+key);
                if ( typeof bakFunc == 'function'){
                    bakFunc(key);
                }else{
                    savePost(key);
                }
                
            },
            success:function(data,status){
              //  console.log(data);
                if( typeof bakFunc == 'function'){
                    bakFunc(key+1);
                }else{
                    savePost(key+1);
                }
              
            }
        }); 
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    var Cache={
        type:'local',
        setType:function(){
            if(this.type == 'session'){
                return window.sessionStorage;
            }
            if(this.type == 'local'){
                return window.localStorage;
            }
            console.log('类型错误！');
        },
        set:function($key,$value){
          this.setType().setItem($key,$value);
        },
        get:function($key){
          return this.setType().getItem($key)
        }
    }
</script>

</body>

</html>